package com.thinkgem.jeesite.modules.qx.utils;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.qx.dao.QxLeagueMatchesDao;
import com.thinkgem.jeesite.modules.qx.dao.QxMontiorRuleDao;
import com.thinkgem.jeesite.modules.qx.dao.QxTeamDao;
import com.thinkgem.jeesite.modules.qx.entity.QxLeagueMatches;
import com.thinkgem.jeesite.modules.qx.entity.QxMontiorRule;
import com.thinkgem.jeesite.modules.qx.entity.QxTeam;

public class QxCacheUtils {
	
	private static QxLeagueMatchesDao qxLeagueMatchesDao = SpringContextHolder.getBean(QxLeagueMatchesDao.class);
	private static QxTeamDao qxTeamDao = SpringContextHolder.getBean(QxTeamDao.class);
	private static QxMontiorRuleDao qxMontiorRuleDao = SpringContextHolder.getBean(QxMontiorRuleDao.class);
	
	public static final String CACHE_LEAGUEMATCHES_LIST = "QxLeagueMatchesList";
	public static final String CACHE_TEAM_LIST = "QxTeamList";
	
	public static final String CACHE_TEAM_KEY = "team_key";
	public static final String CACHE_TEAM_NAME_KEY = "team_name_key";
	public static final String CACHE_LEAGUEMATCHES_KEY = "league_matches_key";
	public static final String CACHE_LEAGUEMATCHES_NAME_KEY = "league_matches_name_key";
	public static final String CACHE_MONTIORRULE_NAME_KEY = "montior_rule_name_key";
	 /*
	  public static List<Office> getOfficeList(){
		@SuppressWarnings("unchecked")
		List<Office> officeList = (List<Office>)getCache(CACHE_OFFICE_LIST);
		if (officeList == null){
			User user = getUser();
			if (user.isAdmin()){
				officeList = officeDao.findAllList(new Office());
			}else{
				Office office = new Office();
				office.getSqlMap().put("dsf", BaseService.dataScopeFilter(user, "a", ""));
				officeList = officeDao.findList(office);
			}
			putCache(CACHE_OFFICE_LIST, officeList);
		}
		return officeList;
	}
	  */
	
	
	/**
	 * 从缓存中获取流量通道信息
	 * 条件：通道代码
	 * 获取不到数据库查询后返回并写入缓存
	 */
	/*
	public static JycFlowOfferChannel getFlowOfferChannel(String channelCode){
		JycFlowOfferChannel entity = (JycFlowOfferChannel) getCache(CACHE_OFFERCHANNEL_KEY+channelCode);
		if(entity == null){
			entity = CacheJycFlowOfferChannel(channelCode);//缓存通道信息
		}
		
		return entity;
	}
	*/
	/**
	 * 根据球队id获取球队
	 * @param teamid
	 * @return
	 */
	public static QxTeam geTeam(String teamid) {
		if (StringUtils.isBlank(teamid)) {
			return null;
		}
		QxTeam entity = (QxTeam)getCache(CACHE_TEAM_KEY + teamid);
		if (null == entity) {
			entity = qxTeamDao.get(teamid.toString());
			if (null != entity) {
				putCache(CACHE_TEAM_KEY+entity.getId(), entity);
			}
		}
		return entity;
	}
	/**
	 * 根据球队id获取球队
	 * @param teamid
	 * @return
	 */
	public static QxTeam geTeamByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		QxTeam entity = (QxTeam)getCache(CACHE_TEAM_NAME_KEY + name);
		if (null == entity) {
			entity = qxTeamDao.getByName(name);
			if (null != entity) {
				putCache(CACHE_TEAM_NAME_KEY + entity.getName(), entity);
			}
		}
		return entity;
	}
	public static QxLeagueMatches getQxLeagueMatches(String leagueid) {
		if (StringUtils.isBlank(leagueid)) {
			return null;
		}
		QxLeagueMatches entity = (QxLeagueMatches)getCache(CACHE_LEAGUEMATCHES_KEY + leagueid);
		if (null == entity) {
			entity = qxLeagueMatchesDao.get(leagueid.toString());
			if (null != entity) {
				putCache(CACHE_LEAGUEMATCHES_KEY+entity.getId(), entity);
			}
		}
		return entity;
	}
	public static QxLeagueMatches getQxLeagueMatchesByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		QxLeagueMatches entity = (QxLeagueMatches)getCache(CACHE_LEAGUEMATCHES_NAME_KEY + name);
		if (null == entity) {
			entity = qxLeagueMatchesDao.getQxLeagueMatchesByName(name); 
			if (null == entity) {
				putCache(CACHE_LEAGUEMATCHES_NAME_KEY+entity.getId(), entity);
			}
		} 
		return entity;
	}
	/**
	 * 获取联赛
	 * @return
	 */
	public static List<QxLeagueMatches> findQxLeagueMatches() {
		List<QxLeagueMatches> leagueMatchesList = (List<QxLeagueMatches>)getCache(CACHE_LEAGUEMATCHES_LIST);
		if (leagueMatchesList == null){
			leagueMatchesList = qxLeagueMatchesDao.findAllList(new QxLeagueMatches());
			if (null != leagueMatchesList) {
				putCache(CACHE_LEAGUEMATCHES_LIST, leagueMatchesList);
			}
		}
		return leagueMatchesList;
	}
	
	/**
	 * 获取球队
	 * @return
	 */
	public static List<QxTeam> findTeams() {
		List<QxTeam> teams = (List<QxTeam>)getCache(CACHE_TEAM_LIST);
		if (teams == null){
			teams = qxTeamDao.findAllList(new QxTeam());
			if (null != teams) {
				putCache(CACHE_TEAM_LIST, teams);
			}
		}
		return teams;
	}
	
	public static Object getCache(String key) {
		return getCache(key, null);
	}
	
	public static Object getCache(String key, Object defaultValue) {
//		Object obj = getCacheMap().get(key);
		Object obj = getSession().getAttribute(key);
		return obj==null?defaultValue:obj;
	}
	
	public static Session getSession(){
		try{
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession(false);
			if (session == null){
				session = subject.getSession();
			}
			if (session != null){
				return session;
			}
//			subject.logout();
		}catch (InvalidSessionException e){
			
		}
		return null;
	}
	public static void putCache(String key, Object value) {
//		getCacheMap().put(key, value);
		getSession().setAttribute(key, value);
	}
	
	public static QxMontiorRule getMontiorRuleByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        QxMontiorRule entity = (QxMontiorRule)getCache(CACHE_MONTIORRULE_NAME_KEY + name);
        if (null == entity) {
            entity = qxMontiorRuleDao.getQxMontiorRuleByName(name); 
            if (null == entity) {
                putCache(CACHE_MONTIORRULE_NAME_KEY+entity.getName(), entity);
            }
        } 
        return entity;
    }
}
